<?php
namespace Admin\Controller;
/**
 * 订单管理
 */
class OrderController extends AdminController{
	
	
	public function index() {
		if ( $this->_userInfo['group_id'] == '5') {
			$this->redirect('Order/myorder');
		} elseif ( $this->_userInfo['group_id'] == '3' || $this->_userInfo['group_id'] == '4') {
			$this->redirect('Order/warehouse');
		}elseif ( $this->_userInfo['group_id'] == '2') {
			$this->redirect('Order/audit');
		}
		$map = array();

		$title = I('get.title');
		if ($title) {
			$map['name'] = array('like',"%$title%");
			$map['cmobile'] = array('like',"%$title%");
		}
	
		if(isset($_GET['status'])){
			$map['ordstatus'] = I('status');
			$status = $map['ordstatus'];
		}else{
			$status = null;
			$map['ordstatus'] = array('in', '1,2,3,4,5,6');
		}
		if ( isset($_GET['time-start']) ) {
			$map['ordtime'][] = array('egt',strtotime(I('time-start')));
		}
		if ( isset($_GET['time-end']) ) {
			$map['ordtime'][] = array('elt',24*60*60 + strtotime(I('time-end')));
		}		
		
		$list = $this->lists('Order', $map, 'id DESC');
		foreach ($list as $key => $val) {	
			$list[$key]['son'] = M('order_goods')->where(array('ordid' => $val['ordid']))->select();	
		}

		$this->assign('_list',$list);
		$this->meta_title = '订单列表';
		$this->assign('status', $status);
		// 记录当前列表页的cookie
		Cookie('__forward__',$_SERVER['REQUEST_URI']);
		
		$this->display();
	}
	
	/**
	 * 我的订单
	 */
	public function myorder() {

		$map = array();
	
		$title = I('get.title');
		if ($title) {
			$map['name'] = array('like',"%$title%");
			$map['cmobile'] = array('like',"%$title%");
		}
	
		if(isset($_GET['status'])){
			$map['ordstatus'] = I('status');
			$status = $map['ordstatus'];
		}else{
			$status = null;
			$map['ordstatus'] = array('in', '1,2,3,4,5,6');
		}
		if ( isset($_GET['time-start']) ) {
			$map['ordtime'][] = array('egt',strtotime(I('time-start')));
		}
		if ( isset($_GET['time-end']) ) {
			$map['ordtime'][] = array('elt',24*60*60 + strtotime(I('time-end')));
		}
		
		$map['userid'] = UID;
		$list = $this->lists('Order', $map, 'id DESC');
		foreach ($list as $key => $val) {
			$list[$key]['son'] = M('order_goods')->where(array('ordid' => $val['ordid']))->select();
		}
	
		$this->assign('_list',$list);
		$this->meta_title = '订单列表';
		$this->assign('status', $status);
		// 记录当前列表页的cookie
		Cookie('__forward__',$_SERVER['REQUEST_URI']);
	
		$this->display();
	}
	
	/**
	 * 审核
	 */
	public function audit() {
	
		$map = array();
	
		$title = I('get.title');
		if ($title) {
			$map['name'] = array('like',"%$title%");
			$map['cmobile'] = array('like',"%$title%");
		}
	
		if(isset($_GET['status'])){
			$map['ordstatus'] = I('status');
			$status = $map['ordstatus'];
		}else{
			$status = null;
			$map['ordstatus'] = array('in', '1,6');
		}
		if ( isset($_GET['time-start']) ) {
			$map['ordtime'][] = array('egt',strtotime(I('time-start')));
		}
		if ( isset($_GET['time-end']) ) {
			$map['ordtime'][] = array('elt',24*60*60 + strtotime(I('time-end')));
		}
	
		$list = $this->lists('Order', $map, 'id DESC');
		foreach ($list as $key => $val) {
			$list[$key]['son'] = M('order_goods')->where(array('ordid' => $val['ordid']))->select();
		}
	
		$this->assign('_list',$list);
		$this->meta_title = '订单列表';
		$this->assign('status', $status);
		// 记录当前列表页的cookie
		Cookie('__forward__',$_SERVER['REQUEST_URI']);
	
		$this->display();
	}
	
	/**
	 * 仓库
	 */
	public function warehouse() {

		$map = array();
	
		$title = I('get.title');
		if ($title) {
			$map['name'] = array('like',"%$title%");
			$map['cmobile'] = array('like',"%$title%");
		}
	
		if(isset($_GET['status'])){
			$map['ordstatus'] = I('status');
			$status = $map['ordstatus'];
		}else{
			$status = null;
			$map['ordstatus'] = array('in', '2,3,4,5');
		}
		if ( isset($_GET['time-start']) ) {
			$map['ordtime'][] = array('egt',strtotime(I('time-start')));
		}
		if ( isset($_GET['time-end']) ) {
			$map['ordtime'][] = array('elt',24*60*60 + strtotime(I('time-end')));
		}
	
		$list = $this->lists('Order', $map, 'id DESC');
		foreach ($list as $key => $val) {
			$list[$key]['son'] = M('order_goods')->where(array('ordid' => $val['ordid']))->select();
		}
	
		$this->assign('_list',$list);
		$this->meta_title = '订单列表';
		$this->assign('status', $status);
		// 记录当前列表页的cookie
		Cookie('__forward__',$_SERVER['REQUEST_URI']);
	
		$this->display();
	}
	
	
	/**
	 * 回收站
	 */
	public function recycle() {
	
		$map = array();
	
		$title = I('get.title');
		if ($title) {
			$map['name'] = array('like',"%$title%");
			$map['cmobile'] = array('like',"%$title%");
		}
	
		if(isset($_GET['status'])){
			$map['ordstatus'] = I('status');
			$status = $map['ordstatus'];
		}else{
			$status = null;
			$map['ordstatus'] = array('in', '7');
		}
		if ( isset($_GET['time-start']) ) {
			$map['ordtime'][] = array('egt',strtotime(I('time-start')));
		}
		if ( isset($_GET['time-end']) ) {
			$map['ordtime'][] = array('elt',24*60*60 + strtotime(I('time-end')));
		}
	
		$list = $this->lists('Order', $map, 'id DESC');
		foreach ($list as $key => $val) {
			$list[$key]['son'] = M('order_goods')->where(array('ordid' => $val['ordid']))->select();
		}
	
		$this->assign('_list',$list);
		$this->meta_title = '订单列表';
		$this->assign('status', $status);
		// 记录当前列表页的cookie
		Cookie('__forward__',$_SERVER['REQUEST_URI']);
	
		$this->display();
	}
	
	
	/**
	 * 下订单[业务员权限]
	 */
	public function add($cid = NULL) {
		if (empty($cid)) $this->error('参数不能为空！');
		if ($cid) {
			$customer = D('Customer')->info($cid);
		}
		$map['status']  = 1;

		$this->assign('customer', $customer);
		$list = M('Goods')->where($map)->field('id,title')->select();
		$this->assign('_list', $list);
		$this->meta_title = '新增客户订单';
		$this->display();
	}

	
	/**
	 * 发货
	 */
	public function sipping($id = NULL) {
		if (empty($id)) $this->error('参数不能为空！');
		
		$info = D('Order')->info($id);	
		$info['goods'] = D('order_goods')->where(array('ordid' => $info['ordid']))->select();
		
		if (IS_POST) {
			$map['id'] = $id;
	
			if ( isset($_GET['sendtime']) ) {
				$sendtime = strtotime(I('sendtime'));
			} else {
				$sendtime = NOW_TIME;
			}

			$data = array(
					'exp_name'	 => I('exp_name'),
					'exp_number' => I('exp_number'),
					'sendtime'	 => $sendtime,
					'ordstatus'  => 3,
					);
			$result = M('Order')->where($map)->save($data);
			if(!$result){
				$this->error(M('Order')->getError());
			}else{
				//减去库存
				foreach ($info['goods'] as $key => $val) {
					D('Goods')->upNumber($val['good_id'], 'stock', $val['ordbuynum'], -1);
				}
				
				$this->success('发货成功，客户收货后请确定收货！', Cookie('__forward__'));
			}
		} else {
			$this->assign('info', $info);
			$this->display();
		}

	}
	
	/**
	 * 退货
	 * @param unknown_type $id
	 */
	public function refund($id = NULL) {
		$info = D('Order')->info($id);
		$goods = D('order_goods')->where(array('ordid' => $info['ordid']))->select();
		
		//加库存
		foreach ($goods as $key => $val) {
			D('Goods')->upNumber($val['good_id'], 'stock', $val['ordbuynum'], 1);
		}
		
		$data['ordstatus'] = 5;
		$data['thtime'] = time();
		$result = M('Order')->where(array('id' => $id))->save($data);
		if (!$result) {
			$this->error('退货失败');
		} else {
			$this->success('操作成功，订单已取消！', Cookie('__forward__'));
		}
	}
	
	
	/**
	 * 更新或新增一条数据
	 */
	public function update(){

		//客户信息
		$cid = I('cid');
		$customerInfo = D('Customer')->info($cid);

		//取多个商品信息
		$namesarr = I('namesarr'); //商品标题
		$goodsids = I('goodsids'); //商品ID
		$stockarr = I('stockarr'); //订货数量
		$price = I('pricearr'); //订货数量
		if (empty($goodsids)) 
			$this->error('请选择商品！');
		
		//收货人信息
		$phone= I('phone');
		$address = I('address');
		$name = I('name');
		//处理订货详情
		for ($i =0 ; $i < count($goodsids); $i++) {
			$goods[$i]['name'] = $namesarr[$i];	
			$goods[$i]['id']   = $goodsids[$i];
			$goods[$i]['stock'] =$stockarr[$i];
			$goods[$i]['price']  = $price[$i];
		}

		//计算总交易额
		foreach ($goods as $key => $val) {
			$ordfee +=  $val['stock'] * $val['price'];
		} 
	
		//处理业务员
		$_POST['userid'] = $customerInfo['uid'];
		$_POST['username'] = M('Member')->where(array('uid' => $_POST['userid']))->getField('nickname');

		//所属业务员的客户
		//没有做
		$ordid = ordersn(); //订单号
		$_POST['cid'] = $cid;
		$_POST['ordid'] = $ordid;
		$_POST['cmobile'] = $phone ? $phone : $customerInfo['phone'];
		$_POST['address'] = $address ? $address : $customerInfo['address'];
		$_POST['name'] = $name ? $name : $customerInfo['name'];
		$_POST['ordfee'] = $ordfee;

		$Order = D('Order');
		$res = $Order->update();
		if(!$res){
			$this->error($Order->getError());
		}else{
			//添加订单和产品关联
			foreach ($goods as $key => $val) {
				
				$data = array(
						'ordid'			=> $ordid,
						'good_id' 		=> $val['id'],
						'good_title' 	=> $val['name'],
						'ordbuynum' 	=> $val['stock'],
						'ordprice' 		=> $val['price'],
				);
				$result = M('order_goods')->add($data);
				
			}
			$this->success($res['id']?'订单更新成功':'订单提交成功', Cookie('__forward__'));
		}
	}
	
	
}